/**   
* @Package com.app.open.sys.menu.web 
*/
package com.wlyy.bcwlw.sys.menu.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wlyy.bcwlw.base.BaseController;
import com.wlyy.bcwlw.common.tree.TreeNode;
import com.wlyy.bcwlw.sys.application.dao.ApplicationDAO;
import com.wlyy.bcwlw.sys.menu.dao.MenuDAO;
import com.wlyy.bcwlw.sys.menu.dao.ModuleDAO;
import com.wlyy.bcwlw.sys.menu.entity.MenuDTO;
import com.wlyy.bcwlw.sys.menu.service.MenuService;


/** 
 * <p>Description: TODO</p>
 * @author zhangyang
 * <p>日期:2015年6月25日 下午5:53:36</p> 
 * @version V1.0 
 */
@RemoteProxy
@Controller
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController{
	@Autowired
	private MenuService menuService;
	@Autowired
	private ApplicationDAO applicationDAO;
	@Autowired
	private MenuDAO menuDAO;
	@Autowired
	private ModuleDAO moduleDAO;
	
	@RequestMapping("/view")
	public String menuList(HttpServletRequest request, HttpServletResponse response){
		return "sys/menu/menu-index";
	}
	
	
	
	@RequestMapping("/checkNameISExist")
	public String checkNameISExist(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String name = get(request,"editMenu_name");
		String moduleId = get(request,"moduleId");
		String id = get(request,"id");
		
		MenuDTO menuDTO = new MenuDTO();
		menuDTO.setModuleId(moduleId);
		menuDTO.setName(name);
		int count = menuDAO.count(menuDTO);
		String str ="true";
		if(count >0){
			str="false";
		}
		if(StringUtils.isNotEmpty(id)){
			menuDTO = menuDAO.findById(id);
			if(menuDTO!=null&&menuDTO.getName().equals(name)){
				str="true";
			}
		}
		sendMessage(response, str);
		return null;
	}
	
	@RequestMapping("/iconChoice")
	public String iconChoice(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String nowIcon = get(request,"nowIcon");
		set(request,"nowIcon",nowIcon);
		return "sys/menu/menu-icon-choice";
	}
	
	
	
	public TreeNode getTreeByAppId(String id,Boolean display){
		return menuService.getMenuTree(id, display);
	}
	
	public String menuAllTree(boolean display){
		return menuService.getAllMenu(display);
	}
	
	public MenuDTO getMenuInfoById(String menuId){
		if(StringUtils.isEmpty(menuId)){
			return null;
		}
		MenuDTO menu = menuService.queryWithModuleNameById(menuId);
		return menu;
	}
	
	public MenuDTO saveMenuInfo(MenuDTO menuDTO){
		MenuDTO newMenuDTO = menuService.save(menuDTO);
		return newMenuDTO;
	}
	
	public String deleteMenuById(String menuId){
		boolean value = menuService.deleteMenuById(menuId);
		if(value){
			return "success";
		}
		return "fail";
	}
	
	

	
	

	
	
	
	
}
